/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.wizards.jcproject;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipsejcde.wizards.jcproject.pages.NewJCProjectJavaCapabilityConfigurationPage;
import org.eclipsejcde.wizards.jcproject.pages.NewJCProjectNameWizardPage;

/**
 * @author Amgad S Youssef
 *
 */
public class NewJCProjectWizard extends Wizard implements INewWizard {
	private NewJCProjectNameWizardPage newJCProjectNameWizardPage;

	private JavaCapabilityConfigurationPage javaCapabilityConfigurationPage;

	public NewJCProjectWizard() {
		super();
		setNeedsProgressMonitor(true);
		setWindowTitle("New Java Card Project");
	}

	@Override
	public boolean performFinish() {
		try {
			if (javaCapabilityConfigurationPage.getJavaProject() == null) {
				IJavaProject jcProject = newJCProjectNameWizardPage.getCreatedJCProject();
				if (jcProject == null || !jcProject.exists()) {
					newJCProjectNameWizardPage.createJavacardProject(null);
					jcProject = newJCProjectNameWizardPage.getCreatedJCProject();
				}
				javaCapabilityConfigurationPage.init(jcProject, jcProject.getOutputLocation(), jcProject.getRawClasspath(), true);
			}
			javaCapabilityConfigurationPage.configureJavaProject(null);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog.openError(getShell(), "Error", e.getMessage());
		}
		return false;
	}

	@Override
	public boolean performCancel() {
		if (newJCProjectNameWizardPage.getProjectName() == null || newJCProjectNameWizardPage.getProjectName().equals("")) {
			return super.performCancel();
		}
		try {
			IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(newJCProjectNameWizardPage.getProjectName());
			if (iProject.exists()) {
				iProject.delete(true, true, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog.openError(getShell(), "Error", e.getMessage());
		}
		return super.performCancel();
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		newJCProjectNameWizardPage = new NewJCProjectNameWizardPage();
		javaCapabilityConfigurationPage = new NewJCProjectJavaCapabilityConfigurationPage(newJCProjectNameWizardPage);
		javaCapabilityConfigurationPage.setTitle("Java Card Settings");
		javaCapabilityConfigurationPage.setDescription("Define Java Card project build settings.");

	}

	@Override
	public void addPages() {
		addPage(newJCProjectNameWizardPage);
		addPage(javaCapabilityConfigurationPage);
	}
}
